//
//  退货详情Ctr.swift
//  smosky
//
//  Created by zhuxietong on 2018/8/7.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import soapp
import Eelay
import AsyncDisplayKit



public class GoodsRefundCtr: ASListController{
    
    public var sections:TP.section{
        get{
            let sec:TP.section = [
                [
                    ["cellID":"status","status":"83","__model":mo.model],
                    ["cellID":"note"],
                    ["cellID":"history","title":"协商历史".loc],
                ],
                [
                    ["cellID":"wuliu","__model":mo.model]
                ],
                [
                    ["cellID":"blank","height":"8","hex":UIColor.table_bg_hex],
                    ["cellID":"title","title":"退货信息".loc],
                    ["cellID":"goods","__model":mo.model],
                    ["cellID":"apply"],

                ],
                [
                ]
            ]
            return sec
        }
    }
    
    let selector:TP.selector = [
        "cellID<status>":StatusCell.self,
        "cellID<actions>":Goods.BtsStatusCell.self,
        "cellID<note>":SaleAfter.NoteCell.self,
        "cellID<history>":SaleAfter.HistoryCell.self,
        "cellID<blank>":BlankNode.self,
        "cellID<goods>":SaleAfter.GoodsCell.self,
        "cellID<title>":SaleAfter.TitleCell.self,
        "cellID<apply>":SaleAfter.ApplayInfoCell.self,
        "cellID<wuliu>":SaleAfter.LogisticsCell.self,
    ]
    
    
    var returnID:String{
        get{
            return ""
        }
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        title = "退货信息".loc
        mo.detail = mo.model
        
        tableNode.backgroundColor = UIColor.table_bg_color
    }
    
//    override func mo_request_send() {
//
//        weak var wself = self
//        loadingV.loading()
//        JoTask().get.log.params(["orderid":returnID])._url(.订单详情).log_response().json_handle { (success, msg, obj, resp) in
//            wself?.loadingV.dismiss()
//            if success{
//                if let dict = obj as? NSMutableDictionary{
//                    wself?.mo.detail = dict
//                    return
//                }
//            }
//            wself?.mo_request_alert(msg: msg, title: "", bt_title: "重试")
//        }.run()
//    }
    
    public override func mo_detail_reload() {
        tableNode.load(sections: sections, selector: selector)
        tableNode.reloadData()
        //        let msgid = MsgMap.getMsgID(type: .order, objID: self.orderID)
        
    }
    
    
}

extension GoodsRefundCtr{
    
    
    
    class StatusCell: JoCellNode {
        let nameL = JoTextNode()
        let infoL = JoTextNode()
        let timeL = JoTextNode()
        var status:AfterStatus = .none
        
        let bt = ASButtonNode()
        
        
        let noteL = JoTextNode()
        override func mo_model_reload() {
            backgroundColor = ._background

            let order = mo.model
            self.separatorColor = UIColor.separator_color
            self.separatorHeight = TP.separatorHeight
            
            self.status = AfterStatus(rawValue: order["status",""]) ?? .none
            let info = status.barInfo
            nameL.attributedText = info.name.ui.bfont16.cl_fff.astring
            infoL.attributedText = status.statusSubtitle(obj: order)
            timeL.attributedText = "剩余10天5时4分".ui.font12.cl_fff.astring
            
            let noteInfo = status.noteText(obj: mo.model)
            noteL.attributedText = noteInfo.text
            noteBk.backgroundColor = noteInfo.backgroundColor
            
            colorBk.backgroundColor = info.color
            
            if info.bt.len > 0
            {
                bt.ui.font(size: 12).ncolor(hex: "#fff").padding(insets: [5,7]).ntitle(title: info.bt).set(radius: 2, width: 1, color: .white)
                bt.addTarget(self, action: #selector(doAction(sender:)), forControlEvents: .touchUpInside)
            }
            else{
                bt.alpha = 0
            }
        }
        
        @objc func doAction(sender:ASButtonNode) {
            weak var wself = self
            var model = mo.model
            
            if let __model = DataNode<NSMutableDictionary>.path(node: "__model", mo.model)
            {
                
                model = __model
            }
            self.status.doAction(with: sender.actionID, cell: wself, at: content_controller, order: model)
        }
        
        let colorBk = ASDisplayNode()
        let noteBk = ASDisplayNode()

        override func specGetter() -> _Spec {
            var specs:[_Spec] = [_Spec.stack(["h|10",&-.center,_justfy(.space_between)], childs: [
                nameL.Spec,bt.Spec
                ]),infoL.Spec]
            if let _ = status.limitHours
            {
                specs.append(timeL.Spec)
            }
            let spec = _Spec.stack(["v|8",_justfy(.space_between),[20,AppTheme.edge],&-.stretch], childs: specs)
            let content = _Spec.back([], over: spec, back: colorBk.Spec)
            var root_childs:[_Spec] = [content]
            if status.noteText(obj: mo.model).show{
                
                if status == AfterStatus.商家同意退货
                {
                    let note:_Spec = .inset([], [10,AppTheme.edge], child: noteL.Spec)
                    root_childs.insert(.back([], over: note, back: noteBk.Spec), at: 0)
//                    root_childs.append(.back([], over: note, back: noteBk.Spec))
                }
                else{
                    let note:_Spec = .inset([], [10,AppTheme.edge], child: noteL.Spec)
                    root_childs.append(.back([], over: note, back: noteBk.Spec))
                }
            
            }
            
            let root = _Spec.stack(["v",&-.stretch], childs: root_childs)
            return root
        }
    }
}
